<?php
namespace XtwsBack\Controller;
use Think\Controller;
class WithDrawXCController extends CommonController {

    // 门店拼团支付金额提现
    public function index(){

        $member  = M('member');
        // 查询余额不为0的店铺老板信息
        $map['u_type'] = 2;
        if (IS_POST){
            $map['id'] = M("member")->where("nickname LIKE '%".$_POST['nickname']."%'")->getField('id');//店主id
        }else{
            $map['credit3'] = array('neq','0.00');
        }//end if
        $map['status'] = array('eq','0');
        $count = $member->where($map)->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,100);// 实例化分页类 传入总记录数和每页显示的记录数(25),查出一百条记录到前台，再由前台js处理成分页
        $show = $Page->show();// 分页显示输出
        $list = $member ->order('id desc')->where($map)->limit($Page->firstRow.','.$Page->listRows)->field('nickname,realname,credit3,id')->select();
//        foreach ($list as $k => $v){
//            $list[$k]['storename'] = M('shop_store')->where('u_id ='.$v['id'])->getField('storename');  //店铺名称
//        }
        $this -> assign('list',$list);
        $this -> assign('page',$show);
        //加载视图
        $this->display();
    }//end fun

    public function loginfo(){
        $u_id = I('u_id');
//        $a_date = $_GET['date'];  //获取日期

        $member = M('member');
//        $shop_pay_log = M('shop_pay_log');
        $shop_spellgroup_order = M("shop_spellgroup_order");

//        $date = explode("-",$a_date);
//        $year = $date[0];
//        $month = $date[1];
//        $day = $date[2];
//        $s_time = mktime(0,0,0,$month,$day,$year);      //当天开始时间戳
//        $e_time= mktime(23,59,59,$month,$day,$year);    //当天结束时间戳

        //查询条件
//        $store = M("shop_store")->where("u_id={$u_id} AND status = 1")->getField('id');//店铺id

        $spellgroup = M("shop_spellgroup")->where("u_id = {$u_id} AND type = 0")->field('id')->select();//所有发布的商品

        $spellgroup_copy=array();
        foreach ($spellgroup as $key => $value){
            array_push($spellgroup_copy,M("shop_spellgroup_copy")->where("sg_id = {$value['id']}")->getField('id'));
        }//end foreach
//        dump($store);
//        dump($spellgroup);
//        dump($spellgroup_copy);
        if($spellgroup_copy){
            $where['c_id']=array('in',$spellgroup_copy);
            $where['state'] = array('in',array(2,7));
            $where['canceltime']=array("gt",0);
//            $where['paytime'] = array('between',array($s_time,$e_time));

            //微信支付
            $wxpay_arr = $shop_spellgroup_order->where($where)->select();   //收款明细
//        dump($where);

            $a_date ='';
            $arr_id = array();
            $wxpay_list = array();
            foreach ($wxpay_arr as $k=>$v){
                $a_time = date("Y-m-d",$v['canceltime']);
                $wxpay_arr[$k]['is_tx'] = M('member_credit_log')->where("`u_id` = {$u_id} AND `shop_credit` = 12 AND `clearingdate` LIKE '{$a_time}'" )->getField('id');
                if($wxpay_arr[$k]['is_tx']){
                    array_push($arr_id,$v['id']);
                }//end if
            }//end foreach

            foreach ($wxpay_arr as $k=>$v){
                $a_time = date("Y-m-d",$v['canceltime']);
                $wxpay_list[strtotime($a_time)][]=$v;
            }//end foreach
            krsort($wxpay_list);

            foreach ($wxpay_list as $k=>$v){
                foreach ($v as $key=>$val){
                    $wxpay_list[$k][$key]['fee']= ($val['total_fee']+$val['card_fee']);
                    $wxpay_list[$k][$key]['sx_fee'] = round($wxpay_list[$k][$key]['fee']*0.003, 2)+round($wxpay_list[$k][$key]['fee']*0.007, 2)+round($wxpay_list[$k][$key]['fee']*0.006, 2);//手续费
                    $wxpay_list[$k]['wxpay_total'] += $wxpay_list[$k][$key]['fee'];     //微信收款总金额
                    if($val['is_tx']<=0){
                        $wxpay_list[$k]['wx_total_fee']  += $wxpay_list[$k][$key]['sx_fee']; //手续费求和
                    }//end if
                }//end foreach
//                $wxpay_list[$k]['integral_fee'] = round($v['wxpay_total']*0.007, 2);          //积分费四舍五入
//                $wxpay_list[$k]['wx_service_fee'] = round($v['wxpay_total']*0.006, 2);          //积分费四舍五入
            }//end foreach

//            dump($wxpay_arr);
//            dump($wxpay_list);
            if ($arr_id){
                $where['id']=array('not in',$arr_id);
            }//end if
            $wxpay_total = $shop_spellgroup_order->where($where)->sum('total_fee')+$shop_spellgroup_order->where($where)->sum('card_fee');     //微信收款总金额
            $wxpay_fee = round($wxpay_total*0.003, 2);          //积分费四舍五入
            $integral_fee = round($wxpay_total*0.007, 2);          //积分费四舍五入
            $service_fee = round($wxpay_total*0.006, 2);          //积分费四舍五入
            $data['wxpay_fee'] = $wxpay_fee;
            $data['integral_fee'] = $integral_fee;
            $data['service_fee'] = $service_fee;

            //微信可提现金额
            $data['wx_total_fee'] = round($wxpay_total-$wxpay_fee-$integral_fee-$service_fee,2);
//        //支付宝可提现金额
//        $data['ali_total_fee'] = round($alipay_total-$alipay_fee,2);
        }//end if



//        //线下扫码-支付宝支付
//        $map = $where;
//        $map['pay_type'] = 'alipay';
//        $alipay_list = $shop_pay_log->where($map)->select();    //收款明细表
//        $alipay_fee = 0;
//        foreach ($alipay_list as $k=>$v){
//            $fee = $v['amount']*0.003;
//            $fee = round($fee, 2);
//            $alipay_list[$k]['sx_fee'] = round($fee, 2);
//            $alipay_fee += $fee;  //手续费求和
//        }
//
//        $data['alipay_fee'] = $alipay_fee;
//        $alipay_total = $shop_pay_log->where($map)->sum('amount');      //支付宝收款总金额

        //查询用户是否已经提现
//        $data['is_tx'] = M('member_credit_log')->where("`u_id` = {$u_id} AND `shop_credit` = 10 AND `clearingdate` LIKE '{$a_date}'" )->getField('id');
//        dump($data);
        $m_info  = $member->where("id={$u_id}")->field('nickname,realname,credit3,id')->find();

        $this -> assign('date',$a_date);
        $this -> assign('wxpay_list',$wxpay_list);
//        $this -> assign('alipay_list',$alipay_list);
        $this -> assign('wxpay_total',$wxpay_total);
//        $this -> assign('alipay_total',$alipay_total);
        $this -> assign('m_info',$m_info);
        $this -> assign($data);
        //加载视图
        $this->display();
    }// end fun

    // 提现-余额变动
    public function changeCredit3(){
        if(IS_POST){
            $post =  I('post.');
            $u_id = $post['u_id'];
            $date = date('Y-m-d',$post['date']);

//            dump($date);die;
            // 查询用户是否已提现
            $is_tx = M('member_credit_log')->where("`u_id` = {$u_id} AND `shop_credit` = 12 AND `clearingdate` LIKE '{$date}'" )->getField('id');
            if($is_tx>0){
                $this->ajaxReturn(array('status'=>1,'msg'=>'已提现'),"JSON");
            }else{
                // 用户余额减少
                $res = M('member')->where('id = '.$post['u_id'])->setDec('credit3',$post['credit3']);
                if($res){
//                        增加余额变动记录
                    $result = changeCredit($post['u_id'],'提现（乡村拼团支付）','credit3','-',$post['credit3'],12,$date);
                    $this->ajaxReturn(array('status'=>0,'msg'=>'提现成功'),"JSON");
                }//end if
            }//end if
        }else{
            $this->error('非法进入');
        }//end if
    }//end fun


}// end class